<template>
  <div class="auth-container">
    <el-form :model="form" class="auth-form" @submit.prevent="register">
      <h3>创建账户</h3>
      <p class="subtitle">欢迎加入，请填写信息</p>

      <el-form-item label="用户名" label-width="80px">
        <el-input v-model="form.username" placeholder="请输入用户名" />
      </el-form-item>
      <el-form-item label="密码" label-width="80px">
        <el-input v-model="form.password" type="password" placeholder="请输入密码" show-password />
      </el-form-item>
      <el-button type="primary" native-type="submit" class="submit-button">注册</el-button>

      <p class="login-link">
        已有账号？
        <router-link to="/login">立即登录</router-link>
      </p>
    </el-form>
  </div>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { useAuthStore } from '../store/auth';
import { ElMessage } from 'element-plus';

const form = ref({
  username: '',
  password: '',
});

const authStore = useAuthStore();
const router = useRouter();

const register = async () => {
  try {
    if (!form.value.username || !form.value.password) {
      ElMessage.error('请输入用户名和密码');
      return;
    }
    
    // 用户名长度验证 (3~20个字符)
    if (form.value.username.length < 3 || form.value.username.length > 20) {
      ElMessage.error('用户名必须为3~20个字符');
      return;
    }
    
    // 密码长度验证 (至少6位)
    if (form.value.password.length < 6) {
      ElMessage.error('密码至少6位');
      return;
    }
    
    const result = await authStore.register(form.value.username, form.value.password);
    if (authStore.token) {
      ElMessage.success('注册成功');
      router.push({ name: 'Home' });
    } else {
      ElMessage.error('注册失败，请重试');
    }
  } catch (error) {
    console.error('注册错误:', error);
    ElMessage.error('注册失败，请重试。');
  }
};
</script>

<style scoped>
.auth-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 40px 20px;
  box-sizing: border-box;
}

.auth-form {
  width: 100%;
  max-width: 600px; /* 更宽一些 */
  padding: 50px; /* 增加内边距 */
  background-color: rgba(255, 255, 255, 0.98);
  border-radius: 20px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.2);
  animation: fadeInUp 0.6s ease-out forwards;
}

@keyframes fadeInUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.auth-form h3 {
  text-align: center;
  margin-bottom: 10px;
  font-size: 24px;
  color: #333;
}

.subtitle {
  text-align: center;
  font-size: 14px;
  color: #666;
  margin-bottom: 20px;
}

.submit-button {
  width: 100%;
  margin-top: 10px;
  padding: 12px;
  font-size: 16px;
  border-radius: 8px;
  transition: all 0.3s ease;
  background: linear-gradient(to right, #4f46e5, #6d28d9);
  border: none;
}

.submit-button:hover {
  background: linear-gradient(to right, #3b34d1, #5a24c2);
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.login-link {
  text-align: center;
  margin-top: 15px;
  font-size: 14px;
  color: #666;
}

.login-link a {
  color: #409EFF;
  text-decoration: none;
  font-weight: bold;
  margin-left: 5px;
}

.login-link a:hover {
  text-decoration: underline;
}
</style>